package com.emqx.auth.service;

import com.emqx.auth.dto.EmqxAuthDTO;
import com.emqx.auth.entity.EmqxHttpAuth;
import com.emqx.auth.repository.EmqxAuthRepository;
import com.xiot.common.base.util.BeanMapping;
import com.xiot.common.util.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :  zhangfutao
 * @version :  V-1.0
 * @description :
 * @date :   2021/12/17 16:57
 */
@Slf4j
@Service
public class EmqxAuthServiceImpl implements EmqxAuthService {

    @Resource
    private EmqxAuthRepository emqxAuthRepository;

    @Override
    public EmqxHttpAuth add(EmqxAuthDTO emqxAuthDTO) {
        EmqxHttpAuth emqxHttpAuth = BeanMapping.map(emqxAuthDTO, EmqxHttpAuth.class);
        String id = Md5Util.md5(emqxAuthDTO.getClientId() + emqxAuthDTO.getConnectTime() + emqxAuthDTO.getAuthType());
        emqxHttpAuth.setId(id);
        return emqxAuthRepository.save(emqxHttpAuth);
    }
}
